package com.example.common;

import com.example.exception.ImoocMallExceptionEnum;

/**
 * 描述：统一返回对象
 */

public class ApiRestResponse<T>{
    private Integer status;
    private String msg;
    private T data;
    //    状态码
    private static final int OK_CODE=200;
    //    状态码对应的值
    private static final String OK_MSG="success";
    //    构造方法
    public ApiRestResponse(Integer status, String msg, T data) {
        this.status = status;
        this.msg = msg;
        this.data = data;
    }

    public ApiRestResponse(Integer status, String msg) {
        this.status = status;
        this.msg = msg;
    }

    /**  1
     * 默认的构造函数表示成功
     */
    public ApiRestResponse() {
        this(OK_CODE,OK_MSG);
    }

    /**
     * 定义一个方法返回统一对象成功的方法,调用上面的方法1
     */
    public static <T> ApiRestResponse <T> success(){
        return new ApiRestResponse<>();
    }
    /**
     * 有参构造方法，返回data信息
     */
    public static <T> ApiRestResponse<T> success(T result){
        ApiRestResponse<T> response = new ApiRestResponse<>();
        response.setData(result);
        return response;
    }

    /**
     *
     * @return   返回失败的方法
     * 可以把常用的code和提示信息放到枚举中，方便以后维护
     */
    public static <T> ApiRestResponse<T> error(Integer code,String msg){
        return new ApiRestResponse<>(code,msg);
    }
    public static <T> ApiRestResponse<T> error(ImoocMallExceptionEnum ex){
        return new ApiRestResponse<>(ex.getCode(),ex.getMsg());
    }

    @Override
    public String toString() {
        return "ApiRestResponse{" +
                "status=" + status +
                ", msg='" + msg + '\'' +
                ", data=" + data +
                '}';
    }

    public Integer getStatus() {
        return status;
    }

    public void setStatus(Integer status) {
        this.status = status;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public static int getOkCode() {
        return OK_CODE;
    }

    public static String getOkMsg() {
        return OK_MSG;
    }
}
